﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using HtmlAgilityPack;

namespace WebArchiveCopier
{
	public class Item
	{
		private string copyUri;
		private string uri;
		private DateTime from;
		private DateTime to;
		private int captures;
		private int duplicates;
		private int uniques;

		private static string copyUriFormat = "http://wayback.archive.org{0}";

		public Item(HtmlNode node)
		{
			var uriNode = node.SelectSingleNode("./td[1]/a");
			var dateFromNode = node.SelectSingleNode("./td[@class='dateFrom']");
			var dateToNode = node.SelectSingleNode("./td[@class='dateFrom']");
			var capturesNode = node.SelectSingleNode("./td[@class='captures']");
			var duplicatesNode = node.SelectSingleNode("./td[@class='dupes']");
			var uniquesNode = node.SelectSingleNode("./td[@class='uniques']");

			if (uriNode != null)
			{
				copyUri = string.Format(copyUriFormat, uriNode.GetAttributeValue("href", "").Trim());
				uri = uriNode.InnerText.Trim();
			}
			if (dateFromNode != null) from = DateTime.Parse(dateFromNode.InnerText.Trim());
			if (dateToNode != null) to = DateTime.Parse(dateToNode.InnerText.Trim());
			if (capturesNode != null) captures = int.Parse(capturesNode.InnerText.Trim());
			if (duplicatesNode != null) duplicates = int.Parse(duplicatesNode.InnerText.Trim());
			if (uniquesNode != null) uniques = int.Parse(uniquesNode.InnerText.Trim());
		}

		public string CopyUrl
		{
			get
			{
				return copyUri;
			}
		}

		public string Uri
		{
			get
			{
				return uri;
			}
		}

		public DateTime From
		{
			get
			{
				return from;
			}
		}

		public DateTime To
		{
			get
			{
				return to;
			}
		}

		public int Captures
		{
			get
			{
				return captures;
			}
		}

		public int Duplicates
		{
			get
			{
				return duplicates;
			}
		}

		public int Uniques
		{
			get
			{
				return uniques;
			}
		}
	}
}
